package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.NearbyShopModel;
import com.sanjiahantang.ythshopping.views.NearbyShopView;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZHY_9 on 2017/6/23.
 */

public class NearbyShopPresenter extends BasePresenter {

    private int page;
    private NearbyShopView view;

    public NearbyShopPresenter(NearbyShopView view) {
        this.view = view;
        page = 1;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void getNearbyShopList(final Context context, double longitude, double latitude){
        Map<String, String> param = new HashMap<>();
        param.put("lat", String.valueOf(latitude));
        param.put("lng", String.valueOf(longitude));
        param.put("page", String.valueOf(page));
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "shop/getShops", param, new BaseDelegate.ResultCallback<NearbyShopModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(NearbyShopModel response, Object tag) {
                view.getNearbyShop(response);
            }
        });
    }

    public void getNearbyShopList(final Context context, Map<String, String> param){
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "shop/getShops", param, new BaseDelegate.ResultCallback<NearbyShopModel>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(NearbyShopModel response, Object tag) {
                view.getNearbyShopList(response);
            }
        });
    }
}
